
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>11.线程池与底层原理.md · 计算机 ,java , 大数据 基础知识体系</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="hzh">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../10面试题一.html" />
    
    
    <link rel="prev" href="../blockingQueue/1.阻塞队列.html" />
    

    
    
    
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">操作系统</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../操作系统/1.基本概述.html">
            
                <a href="../../操作系统/1.基本概述.html">
            
                    
                    1.操作系统基本概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../../操作系统/2.进程管理之进程与线程.html">
            
                <a href="../../操作系统/2.进程管理之进程与线程.html">
            
                    
                    2.进程管理之进程与线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../../操作系统/3.进程管理之处理机调度.html">
            
                <a href="../../操作系统/3.进程管理之处理机调度.html">
            
                    
                    3.进程管理之处理机调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../../操作系统/4.进程管理之进程同步.html">
            
                <a href="../../操作系统/4.进程管理之进程同步.html">
            
                    
                    4.进程管理之进程同步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../../操作系统/5.内存管理之基本概念.html">
            
                <a href="../../操作系统/5.内存管理之基本概念.html">
            
                    
                    5.内存管理之基本概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../../操作系统/6.内存管理之虚拟内存.html">
            
                <a href="../../操作系统/6.内存管理之虚拟内存.html">
            
                    
                    6.内存管理之虚拟内存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../../操作系统/7.IO设备管理之基本概念.html">
            
                <a href="../../操作系统/7.IO设备管理之基本概念.html">
            
                    
                    7.IO设备管理之基本概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../../操作系统/8.面试题总结一.html">
            
                <a href="../../操作系统/8.面试题总结一.html">
            
                    
                    8.面试题总结一
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">计算机网络篇</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../../计算机网络/1.计算机网络入门基础概念.html">
            
                <a href="../../计算机网络/1.计算机网络入门基础概念.html">
            
                    
                    1.计算机网络入门文章(必读)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../../计算机网络/2.计算机网络通信基础.html">
            
                <a href="../../计算机网络/2.计算机网络通信基础.html">
            
                    
                    2.计算机网络通信入门文章(必读)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../../计算机网络/3.数据链路层.html">
            
                <a href="../../计算机网络/3.数据链路层.html">
            
                    
                    3.数据链路层
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../../计算机网络/4.网络层1.html">
            
                <a href="../../计算机网络/4.网络层1.html">
            
                    
                    4.网络层(一)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../../计算机网络/5.网络层2.html">
            
                <a href="../../计算机网络/5.网络层2.html">
            
                    
                    5.网络层(二)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../../计算机网络/6.传输层.html">
            
                <a href="../../计算机网络/6.传输层.html">
            
                    
                    6.传输层
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../../计算机网络/7.应用层.html">
            
                <a href="../../计算机网络/7.应用层.html">
            
                    
                    7.应用层(⭐)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../../计算机网络/10.HTTP相关协议.html">
            
                <a href="../../计算机网络/10.HTTP相关协议.html">
            
                    
                    8.HTTP相关及发展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" >
            
                <span>
            
                    
                    9.websocket
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../../计算机网络/9.面试题二.html">
            
                <a href="../../计算机网络/9.面试题二.html">
            
                    
                    面试题总结一
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">数据结构篇</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../../数据结构/1.图论.html">
            
                <a href="../../数据结构/1.图论.html">
            
                    
                    1.图论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../../数据结构/2.图论例题.html">
            
                <a href="../../数据结构/2.图论例题.html">
            
                    
                    2.图论例题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../../数据结构/3.散列表.html">
            
                <a href="../../数据结构/3.散列表.html">
            
                    
                    3.散列表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../../数据结构/4.排序算法.html">
            
                <a href="../../数据结构/4.排序算法.html">
            
                    
                    4.排序算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../../数据结构/5.排序列题.html">
            
                <a href="../../数据结构/5.排序列题.html">
            
                    
                    5.排序算法例题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../../数据结构/6.树论.html">
            
                <a href="../../数据结构/6.树论.html">
            
                    
                    6.树论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../../数据结构/7.树论例题.html">
            
                <a href="../../数据结构/7.树论例题.html">
            
                    
                    7.树论例题.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="../../数据结构/9.模板.html">
            
                <a href="../../数据结构/9.模板.html">
            
                    
                    数据结构模板
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">算法篇</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../../算法/leetCode周赛题解.html">
            
                <a href="../../算法/leetCode周赛题解.html">
            
                    
                    leetCode周赛记录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../../算法/1.时间复杂度.html">
            
                <a href="../../算法/1.时间复杂度.html">
            
                    
                    1.时间复杂度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../../算法/2.深度优先搜索.html">
            
                <a href="../../算法/2.深度优先搜索.html">
            
                    
                    2.深度优先搜索算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../../算法/2.深度优先搜索题目集.html">
            
                <a href="../../算法/2.深度优先搜索题目集.html">
            
                    
                    2.深搜题目集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../../算法/3.广度优先搜索.html">
            
                <a href="../../算法/3.广度优先搜索.html">
            
                    
                    3.广度优先搜索算法
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">JVM篇</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../../jvm/1.jvm内存结构.html">
            
                <a href="../../jvm/1.jvm内存结构.html">
            
                    
                    1.jvm内存结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../../jvm/2.HotSpot虚拟机对象探秘.html">
            
                <a href="../../jvm/2.HotSpot虚拟机对象探秘.html">
            
                    
                    2.HotSpot虚拟机对象探秘.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../../jvm/3.对象存亡与垃圾收集算法.html">
            
                <a href="../../jvm/3.对象存亡与垃圾收集算法.html">
            
                    
                    3.对象存亡与垃圾收集算法.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../../jvm/4.HotSpot的算法细节与垃圾收集器.html">
            
                <a href="../../jvm/4.HotSpot的算法细节与垃圾收集器.html">
            
                    
                    4.HotSpot的算法细节与垃圾收集器.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../../jvm/5.内存分配与回收策略.html">
            
                <a href="../../jvm/5.内存分配与回收策略.html">
            
                    
                    5.内存分配与回收策略.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../../jvm/6.类文件结构.html">
            
                <a href="../../jvm/6.类文件结构.html">
            
                    
                    6.类文件结构.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="../../jvm/7.类加载机制.html">
            
                <a href="../../jvm/7.类加载机制.html">
            
                    
                    7.类加载机制.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="../../jvm/8.jvm逃逸分析.html">
            
                <a href="../../jvm/8.jvm逃逸分析.html">
            
                    
                    8.逃逸分析技术
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">多线程篇</li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="../basic/0.多线程路线图.html">
            
                <a href="../basic/0.多线程路线图.html">
            
                    
                    1.多线程学习路线图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="../basic/0.java内存模型与线程.html">
            
                <a href="../basic/0.java内存模型与线程.html">
            
                    
                    2.java内存模型与线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="../3.并行程序基础.html">
            
                <a href="../3.并行程序基础.html">
            
                    
                    2.并行程序基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="../basic/0.jhappenBefore与jvm指令重排.html">
            
                <a href="../basic/0.jhappenBefore与jvm指令重排.html">
            
                    
                    3.happenBefore与jvm指令重排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="../basic/1.synchronized与volatile.html">
            
                <a href="../basic/1.synchronized与volatile.html">
            
                    
                    4.synchronized与volatile.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="../basic/2.ThreadLocal.html">
            
                <a href="../basic/2.ThreadLocal.html">
            
                    
                    5.ThreadLocal.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="../cas/1.乐观锁与悲观锁.html">
            
                <a href="../cas/1.乐观锁与悲观锁.html">
            
                    
                    6.乐观锁与悲观锁.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="../cas/2.CAS与atomic类.html">
            
                <a href="../cas/2.CAS与atomic类.html">
            
                    
                    7.CAS与atomic类.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.9" data-path="../aqs/1.AQS.html">
            
                <a href="../aqs/1.AQS.html">
            
                    
                    8.AQS.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.10" data-path="../aqs/2.基于AQS的并发工具.html">
            
                <a href="../aqs/2.基于AQS的并发工具.html">
            
                    
                    9.基于AQS的并发工具.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.11" data-path="../blockingQueue/1.阻塞队列.html">
            
                <a href="../blockingQueue/1.阻塞队列.html">
            
                    
                    10.阻塞队列.md
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="6.12" data-path="1.线程池与底层原理.html">
            
                <a href="1.线程池与底层原理.html">
            
                    
                    11.线程池与底层原理.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.13" data-path="../10面试题一.html">
            
                <a href="../10面试题一.html">
            
                    
                    12.面试题一
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.14" data-path="../10面试题二.html">
            
                <a href="../10面试题二.html">
            
                    
                    13.面试题二.md
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">java集合篇</li>
        
        
    
        <li class="chapter " data-level="7.1" data-path="../../basic/collection/1.java集合.html">
            
                <a href="../../basic/collection/1.java集合.html">
            
                    
                    1.集合架构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="../../basic/collection/2.HashMap源码分析.html">
            
                <a href="../../basic/collection/2.HashMap源码分析.html">
            
                    
                    2.hashMap底层原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="../../basic/collection/3.ConcurrentHashMap分析.html">
            
                <a href="../../basic/collection/3.ConcurrentHashMap分析.html">
            
                    
                    3.ConcurrentHashMap分析
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">设计模式篇</li>
        
        
    
        <li class="chapter " data-level="8.1" >
            
                <span>
            
                    
                    1.创造型模式
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="../../设计模式/factory/三种工厂模式介绍.html">
            
                <a href="../../设计模式/factory/三种工厂模式介绍.html">
            
                    
                    三种工厂模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="../../设计模式/factory/实战工厂模式.html">
            
                <a href="../../设计模式/factory/实战工厂模式.html">
            
                    
                    实战工厂模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="../../设计模式/factory/实战抽象工厂模式.html">
            
                <a href="../../设计模式/factory/实战抽象工厂模式.html">
            
                    
                    实战抽象工厂模式
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">mysql篇</li>
        
        
    
        <li class="chapter " data-level="9.1" data-path="../../数据库/mysql/1.b树b+树的底层原理.html">
            
                <a href="../../数据库/mysql/1.b树b+树的底层原理.html">
            
                    
                    1.mysql索引基础之b树与b+树⭐⭐
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="../../数据库/mysql/2.mysql索引.html">
            
                <a href="../../数据库/mysql/2.mysql索引.html">
            
                    
                    2.mysql索引及优化⭐⭐⭐⭐
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="../../数据库/mysql/3.mysql存储引擎.html">
            
                <a href="../../数据库/mysql/3.mysql存储引擎.html">
            
                    
                    3.mysql存储引擎.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.4" data-path="../../数据库/mysql/4.多版本并发控制之mvcc原理.html">
            
                <a href="../../数据库/mysql/4.多版本并发控制之mvcc原理.html">
            
                    
                    4.多版本并发控制之mvcc原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.5" data-path="../../数据库/mysql/4.mysql事务.html">
            
                <a href="../../数据库/mysql/4.mysql事务.html">
            
                    
                    5.mysql事务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.6" data-path="../../数据库/mysql/5.mysql锁机制.html">
            
                <a href="../../数据库/mysql/5.mysql锁机制.html">
            
                    
                    6.mysql的锁机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.7" data-path="../../数据库/mysql/6.mysql面试题一.html">
            
                <a href="../../数据库/mysql/6.mysql面试题一.html">
            
                    
                    7.mysql面试题一
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">高并发中间件篇</li>
        
        
    

    
        
        <li class="header">架构理论篇</li>
        
        
    
        <li class="chapter " data-level="11.1" data-path="../../架构/0.架构演进.html">
            
                <a href="../../架构/0.架构演进.html">
            
                    
                    1.架构演进
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="11.2" data-path="../../架构/0.微服务架构与soa架构.html">
            
                <a href="../../架构/0.微服务架构与soa架构.html">
            
                    
                    2.微服务架构与soa架构
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">高可用系统篇</li>
        
        
    
        <li class="chapter " data-level="12.1" data-path="../../高可用架构/1.限流.html">
            
                <a href="../../高可用架构/1.限流.html">
            
                    
                    1.系统限流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="12.2" data-path="../../高可用系统/2.服务熔断.html">
            
                <a href="../../高可用系统/2.服务熔断.html">
            
                    
                    2.服务熔断
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="12.3" data-path="../../高可用系统/3.服务降级.html">
            
                <a href="../../高可用系统/3.服务降级.html">
            
                    
                    3.服务降级
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">spring-cloud 微服务篇</li>
        
        
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >11.线程池与底层原理.md</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><li><span class="title-icon "></span><a href="#&#x4E00;-&#x7EBF;&#x7A0B;&#x6C60;&#x57FA;&#x7840;"><b></b>&#x4E00; &#x7EBF;&#x7A0B;&#x6C60;&#x57FA;&#x7840;</a></li><ul><li><span class="title-icon "></span><a href="#1&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4F18;&#x52BF;"><b></b>1&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4F18;&#x52BF;</a></li><li><span class="title-icon "></span><a href="#2&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4E3B;&#x8981;&#x53C2;&#x6570;"><b></b>2&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4E3B;&#x8981;&#x53C2;&#x6570;</a></li><li><span class="title-icon "></span><a href="#3&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x6D41;&#x7A0B;"><b></b>3&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x6D41;&#x7A0B;</a></li><li><span class="title-icon "></span><a href="#4&#x963B;&#x585E;&#x961F;&#x5217;"><b></b>4.&#x963B;&#x585E;&#x961F;&#x5217;</a></li><li><span class="title-icon "></span><a href="#5&#x5E38;&#x89C1;&#x7EBF;&#x7A0B;&#x6C60;"><b></b>5.&#x5E38;&#x89C1;&#x7EBF;&#x7A0B;&#x6C60;</a></li><li><span class="title-icon "></span><a href="#6&#x62D2;&#x7EDD;&#x7B56;&#x7565;"><b></b>6.&#x62D2;&#x7EDD;&#x7B56;&#x7565;</a></li></ul><li><span class="title-icon "></span><a href="#&#x4E8C;-execute&#x4E0E;submit"><b></b>&#x4E8C; execute&#x4E0E;submit</a></li><ul><li><span class="title-icon "></span><a href="#1&#x4F7F;&#x7528;submit&#x7684;&#x5751;"><b></b>1.&#x4F7F;&#x7528;submit&#x7684;&#x5751;</a></li><li><span class="title-icon "></span><a href="#3-execute&#x548C;submit&#x7684;&#x533A;&#x522B;"><b></b>3. execute&#x548C;submit&#x7684;&#x533A;&#x522B;</a></li></ul><li><span class="title-icon "></span><a href="#&#x4E09;-&#x7EBF;&#x7A0B;&#x6C60;&#x7EBF;&#x7A0B;&#x590D;&#x7528;&#x539F;&#x7406;"><b></b>&#x4E09; &#x7EBF;&#x7A0B;&#x6C60;&#x7EBF;&#x7A0B;&#x590D;&#x7528;&#x539F;&#x7406;</a></li><ul><li><span class="title-icon "></span><a href="#1&#x6838;&#x5FC3;&#x539F;&#x7406;"><b></b>1.&#x6838;&#x5FC3;&#x539F;&#x7406;</a></li><li><span class="title-icon "></span><a href="#2jdk&#x7EBF;&#x7A0B;&#x6C60;&#x590D;&#x7528;"><b></b>2.jdk&#x7EBF;&#x7A0B;&#x6C60;&#x590D;&#x7528;</a></li></ul></ul></ul></div><a href="#" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h2 id="&#x4E00;-&#x7EBF;&#x7A0B;&#x6C60;&#x57FA;&#x7840;"><a name="&#x4E00;-&#x7EBF;&#x7A0B;&#x6C60;&#x57FA;&#x7840;" class="anchor-navigation-ex-anchor" href="#&#x4E00;-&#x7EBF;&#x7A0B;&#x6C60;&#x57FA;&#x7840;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E00;-&#x7EBF;&#x7A0B;&#x6C60;&#x57FA;&#x7840;" class="plugin-anchor" href="#&#x4E00;-&#x7EBF;&#x7A0B;&#x6C60;&#x57FA;&#x7840;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E00; &#x7EBF;&#x7A0B;&#x6C60;&#x57FA;&#x7840;</h2>
<h3 id="1&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4F18;&#x52BF;"><a name="1&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4F18;&#x52BF;" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4F18;&#x52BF;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4F18;&#x52BF;" class="plugin-anchor" href="#1&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4F18;&#x52BF;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4F18;&#x52BF;</h3>
<p>&#xFF08;1&#xFF09;&#x3001;&#x964D;&#x4F4E;&#x7CFB;&#x7EDF;&#x8D44;&#x6E90;&#x6D88;&#x8017;&#xFF0C;&#x901A;&#x8FC7;&#x91CD;&#x7528;&#x5DF2;&#x5B58;&#x5728;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x964D;&#x4F4E;&#x7EBF;&#x7A0B;&#x521B;&#x5EFA;&#x548C;&#x9500;&#x6BC1;&#x9020;&#x6210;&#x7684;&#x6D88;&#x8017;&#xFF1B;
 &#xFF08;2&#xFF09;&#x3001;&#x63D0;&#x9AD8;&#x7CFB;&#x7EDF;&#x54CD;&#x5E94;&#x901F;&#x5EA6;&#xFF0C;&#x5F53;&#x6709;&#x4EFB;&#x52A1;&#x5230;&#x8FBE;&#x65F6;&#xFF0C;&#x901A;&#x8FC7;&#x590D;&#x7528;&#x5DF2;&#x5B58;&#x5728;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x65E0;&#x9700;&#x7B49;&#x5F85;&#x65B0;&#x7EBF;&#x7A0B;&#x7684;&#x521B;&#x5EFA;&#x4FBF;&#x80FD;&#x7ACB;&#x5373;&#x6267;&#x884C;&#xFF1B;
 &#xFF08;3&#xFF09;     &#x65B9;&#x4FBF;&#x7EBF;&#x7A0B;&#x5E76;&#x53D1;&#x6570;&#x7684;&#x7BA1;&#x63A7;&#x3002;&#x56E0;&#x4E3A;&#x7EBF;&#x7A0B;&#x82E5;&#x662F;&#x65E0;&#x9650;&#x5236;&#x7684;&#x521B;&#x5EFA;&#xFF0C;&#x53EF;&#x80FD;&#x4F1A;&#x5BFC;&#x81F4;&#x5185;&#x5B58;&#x5360;&#x7528;&#x8FC7;&#x591A;&#x800C;&#x4EA7;&#x751F;OOM&#xFF0C;&#x5E76;&#x4E14;&#x4F1A;&#x9020;&#x6210;cpu&#x8FC7;&#x5EA6;&#x5207;&#x6362;&#xFF08;cpu&#x5207;&#x6362;&#x7EBF;&#x7A0B;&#x662F;&#x6709;&#x65F6;&#x95F4;&#x6210;&#x672C;&#x7684;&#xFF08;&#x9700;&#x8981;&#x4FDD;&#x6301;&#x5F53;&#x524D;&#x6267;&#x884C;&#x7EBF;&#x7A0B;&#x7684;&#x73B0;&#x573A;&#xFF0C;&#x5E76;&#x6062;&#x590D;&#x8981;&#x6267;&#x884C;&#x7EBF;&#x7A0B;&#x7684;&#x73B0;&#x573A;&#xFF09;&#xFF09;&#x3002;
 &#xFF08;4&#xFF09;&#x63D0;&#x4F9B;&#x66F4;&#x5F3A;&#x5927;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x5EF6;&#x65F6;&#x5B9A;&#x65F6;&#x7EBF;&#x7A0B;&#x6C60;&#x3002;</p>
<h3 id="2&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4E3B;&#x8981;&#x53C2;&#x6570;"><a name="2&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4E3B;&#x8981;&#x53C2;&#x6570;" class="anchor-navigation-ex-anchor" href="#2&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4E3B;&#x8981;&#x53C2;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4E3B;&#x8981;&#x53C2;&#x6570;" class="plugin-anchor" href="#2&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4E3B;&#x8981;&#x53C2;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x4E3B;&#x8981;&#x53C2;&#x6570;</h3>
<pre><code class="lang-cpp"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ThreadPoolExecutor</span><span class="hljs-params">(<span class="hljs-keyword">int</span> corePoolSize, <span class="hljs-keyword">int</span> maximumPoolSize, <span class="hljs-keyword">long</span> keepAliveTime, TimeUnit unit, BlockingQueue&lt;Runnable&gt; workQueue)</span> </span>{
    <span class="hljs-keyword">this</span>(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
         Executors.defaultThreadFactory(), defaultHandler);
}
</code></pre>
<p>1&#x3001;corePoolSize&#xFF08;&#x7EBF;&#x7A0B;&#x6C60;&#x57FA;&#x672C;&#x5927;&#x5C0F;&#xFF09;&#xFF1A;&#x5F53;&#x5411;&#x7EBF;&#x7A0B;&#x6C60;&#x63D0;&#x4EA4;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x65F6;&#xFF0C;&#x82E5;&#x7EBF;&#x7A0B;&#x6C60;&#x5DF2;&#x521B;&#x5EFA;&#x7684;&#x7EBF;&#x7A0B;&#x6570;&#x5C0F;&#x4E8E;corePoolSize&#xFF0C;&#x5373;&#x4FBF;&#x6B64;&#x65F6;&#x5B58;&#x5728;&#x7A7A;&#x95F2;&#x7EBF;&#x7A0B;&#xFF0C;&#x4E5F;&#x4F1A;&#x901A;&#x8FC7;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7EBF;&#x7A0B;&#x6765;&#x6267;&#x884C;&#x8BE5;&#x4EFB;&#x52A1;&#xFF0C;&#x76F4;&#x5230;&#x5DF2;&#x521B;&#x5EFA;&#x7684;&#x7EBF;&#x7A0B;&#x6570;&#x5927;&#x4E8E;&#x6216;&#x7B49;&#x4E8E;corePoolSize&#x65F6;&#xFF0C;&#x5C06;&#x4EFB;&#x52A1;&#x9001;&#x5165;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E2D;</p>
<p>2&#x3001;maximumPoolSize&#xFF08;&#x7EBF;&#x7A0B;&#x6C60;&#x6700;&#x5927;&#x5927;&#x5C0F;&#xFF09;&#xFF1A;&#x7EBF;&#x7A0B;&#x6C60;&#x6240;&#x5141;&#x8BB8;&#x7684;&#x6700;&#x5927;&#x7EBF;&#x7A0B;&#x4E2A;&#x6570;&#x3002;&#x5F53;&#x961F;&#x5217;&#x6EE1;&#x4E86;&#xFF0C;&#x4E14;&#x5DF2;&#x521B;&#x5EFA;&#x7684;&#x7EBF;&#x7A0B;&#x6570;&#x5C0F;&#x4E8E;maximumPoolSize&#xFF0C;&#x5219;&#x7EBF;&#x7A0B;&#x6C60;&#x4F1A;&#x521B;&#x5EFA;&#x65B0;&#x7684;&#x7EBF;&#x7A0B;&#x6765;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#x3002;&#x53E6;&#x5916;&#xFF0C;&#x5BF9;&#x4E8E;&#x65E0;&#x754C;&#x961F;&#x5217;&#xFF0C;&#x53EF;&#x5FFD;&#x7565;&#x8BE5;&#x53C2;&#x6570;&#x3002;</p>
<p>3&#x3001;keepAliveTime&#xFF08;&#x7EBF;&#x7A0B;&#x5B58;&#x6D3B;&#x4FDD;&#x6301;&#x65F6;&#x95F4;&#xFF09;&#x5F53;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x7EBF;&#x7A0B;&#x6570;&#x5927;&#x4E8E;&#x6838;&#x5FC3;&#x7EBF;&#x7A0B;&#x6570;&#x65F6;&#xFF0C;&#x7EBF;&#x7A0B;&#x7684;&#x7A7A;&#x95F2;&#x65F6;&#x95F4;&#x5982;&#x679C;&#x8D85;&#x8FC7;&#x7EBF;&#x7A0B;&#x5B58;&#x6D3B;&#x65F6;&#x95F4;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x7EBF;&#x7A0B;&#x5C31;&#x4F1A;&#x88AB;&#x9500;&#x6BC1;&#xFF0C;&#x76F4;&#x5230;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x7684;&#x7EBF;&#x7A0B;&#x6570;&#x5C0F;&#x4E8E;&#x7B49;&#x4E8E;&#x6838;&#x5FC3;&#x7EBF;&#x7A0B;&#x6570;&#x3002;</p>
<p>4&#x3001;workQueue&#xFF08;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#xFF09;&#xFF1A;&#x7528;&#x4E8E;&#x4F20;&#x8F93;&#x548C;&#x4FDD;&#x5B58;&#x7B49;&#x5F85;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#x7684;&#x963B;&#x585E;&#x961F;&#x5217;&#x3002;</p>
<p>5&#x3001;threadFactory&#xFF08;&#x7EBF;&#x7A0B;&#x5DE5;&#x5382;&#xFF09;&#xFF1A;&#x7528;&#x4E8E;&#x521B;&#x5EFA;&#x65B0;&#x7EBF;&#x7A0B;&#x3002;threadFactory&#x521B;&#x5EFA;&#x7684;&#x7EBF;&#x7A0B;&#x4E5F;&#x662F;&#x91C7;&#x7528;new Thread()&#x65B9;&#x5F0F;&#xFF0C;threadFactory&#x521B;&#x5EFA;&#x7684;&#x7EBF;&#x7A0B;&#x540D;&#x90FD;&#x5177;&#x6709;&#x7EDF;&#x4E00;&#x7684;&#x98CE;&#x683C;&#xFF1A;pool-m-thread-n&#xFF08;m&#x4E3A;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x7F16;&#x53F7;&#xFF0C;n&#x4E3A;&#x7EBF;&#x7A0B;&#x6C60;&#x5185;&#x7684;&#x7EBF;&#x7A0B;&#x7F16;&#x53F7;&#xFF09;&#x3002;</p>
<p>6&#x3001;handler&#xFF08;&#x7EBF;&#x7A0B;&#x9971;&#x548C;&#x7B56;&#x7565;&#xFF09;&#xFF1A;&#x5F53;&#x7EBF;&#x7A0B;&#x6C60;&#x548C;&#x961F;&#x5217;&#x90FD;&#x6EE1;&#x4E86;&#xFF0C;&#x518D;&#x52A0;&#x5165;&#x7EBF;&#x7A0B;&#x4F1A;&#x6267;&#x884C;&#x6B64;&#x7B56;&#x7565;&#x3002;</p>
<h3 id="3&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x6D41;&#x7A0B;"><a name="3&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x6D41;&#x7A0B;" class="anchor-navigation-ex-anchor" href="#3&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x6D41;&#x7A0B;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="3&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x6D41;&#x7A0B;" class="plugin-anchor" href="#3&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x6D41;&#x7A0B;"><i class="fa fa-link" aria-hidden="true"></i></a>3&#x3001;&#x7EBF;&#x7A0B;&#x6C60;&#x6D41;&#x7A0B;</h3>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201211222821.png" alt="image-20201211222821397"></p>
<p>&#x7EBF;&#x7A0B;&#x6C60;&#x6D41;&#x7A0B;</p>
<p>1&#x3001;&#x5224;&#x65AD;&#x6838;&#x5FC3;&#x7EBF;&#x7A0B;&#x6C60;&#x662F;&#x5426;&#x5DF2;&#x6EE1;&#xFF0C;&#x6CA1;&#x6EE1;&#x5219;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x6765;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#x3002;&#x5DF2;&#x6EE1;&#x5219;&#x3002;
 2&#x3001;&#x5224;&#x65AD;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x662F;&#x5426;&#x5DF2;&#x6EE1;&#xFF0C;&#x6CA1;&#x6EE1;&#x5219;&#x5C06;&#x65B0;&#x63D0;&#x4EA4;&#x7684;&#x4EFB;&#x52A1;&#x6DFB;&#x52A0;&#x5728;&#x5DE5;&#x4F5C;&#x961F;&#x5217;&#xFF0C;&#x5DF2;&#x6EE1;&#x5219;&#x3002;
 3&#x3001;&#x5224;&#x65AD;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x6570;&#x662F;&#x5426;&#x5927;&#x4E8E;&#x6838;&#x5FC3;&#x7EBF;&#x7A0B;&#x6570;&#xFF0C;&#x5982;&#x679C;&#x5C0F;&#x4E8E;&#xFF0C;&#x5728;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x7EBF;&#x7A0B;&#x6765;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#xFF0C;&#x5982;&#x679C;&#x5927;&#x4E8E;&#x5219;&#x6267;&#x884C;&#x9971;&#x548C;&#x7B56;&#x7565;&#x3002;</p>
<p>&#x4E3E;&#x4E2A;&#x6817;&#x5B50;&#xFF1A;&#x73B0;&#x6709;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x6C60;&#xFF0C;corePoolSize=10&#xFF0C;maxPoolSize=20&#xFF0C;&#x961F;&#x5217;&#x957F;&#x5EA6;&#x4E3A;100&#xFF0C;&#x90A3;&#x4E48;&#x5F53;&#x4EFB;&#x52A1;&#x8FC7;&#x6765;&#x4F1A;&#x5148;&#x521B;&#x5EFA;10&#x4E2A;&#x6838;&#x5FC3;&#x7EBF;&#x7A0B;&#x6570;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x8FDB;&#x6765;&#x7684;&#x4EFB;&#x52A1;&#x4F1A;&#x8FDB;&#x5165;&#x5230;&#x961F;&#x5217;&#x4E2D;&#x76F4;&#x5230;&#x961F;&#x5217;&#x6EE1;&#x4E86;&#xFF0C;&#x4F1A;&#x521B;&#x5EFA;&#x989D;&#x5916;&#x7684;&#x7EBF;&#x7A0B;&#x6765;&#x6267;&#x884C;&#x4EFB;&#x52A1;(&#x6700;&#x591A;20&#x4E2A;&#x7EBF;&#x7A0B;)&#xFF0C;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x5982;&#x679C;&#x518D;&#x6765;&#x4EFB;&#x52A1;&#x5C31;&#x4F1A;&#x6267;&#x884C;&#x62D2;&#x7EDD;&#x7B56;&#x7565;&#x3002;</p>
<h3 id="4&#x963B;&#x585E;&#x961F;&#x5217;"><a name="4&#x963B;&#x585E;&#x961F;&#x5217;" class="anchor-navigation-ex-anchor" href="#4&#x963B;&#x585E;&#x961F;&#x5217;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="4&#x963B;&#x585E;&#x961F;&#x5217;" class="plugin-anchor" href="#4&#x963B;&#x585E;&#x961F;&#x5217;"><i class="fa fa-link" aria-hidden="true"></i></a>4.&#x963B;&#x585E;&#x961F;&#x5217;</h3>
<ul>
<li>SynchronousQueue(&#x540C;&#x6B65;&#x79FB;&#x4EA4;&#x961F;&#x5217;)&#xFF1A;&#x961F;&#x5217;&#x4E0D;&#x4F5C;&#x4E3A;&#x4EFB;&#x52A1;&#x7684;&#x7F13;&#x51B2;&#x65B9;&#x5F0F;&#xFF0C;&#x53EF;&#x4EE5;&#x7B80;&#x5355;&#x7406;&#x89E3;&#x4E3A;&#x961F;&#x5217;&#x957F;&#x5EA6;&#x4E3A;&#x96F6;</li>
<li>LinkedBlockingQueue(&#x65E0;&#x754C;&#x961F;&#x5217;)&#xFF1A;&#x961F;&#x5217;&#x957F;&#x5EA6;&#x4E0D;&#x53D7;&#x9650;&#x5236;&#xFF0C;&#x5F53;&#x8BF7;&#x6C42;&#x8D8A;&#x6765;&#x8D8A;&#x591A;&#x65F6;(&#x4EFB;&#x52A1;&#x5904;&#x7406;&#x901F;&#x5EA6;&#x8DDF;&#x4E0D;&#x4E0A;&#x4EFB;&#x52A1;&#x5904;&#x7406;&#x901F;&#x5EA6;&#x9020;&#x6210;&#x8BF7;&#x6C42;&#x5806;&#x79EF;)&#x53EF;&#x80FD;&#x5BFC;&#x81F4;&#x5185;&#x5B58;&#x5360;&#x7528;&#x8FC7;&#x591A;&#x6216;OOM</li>
<li>ArrayBlockintQueue(&#x6709;&#x754C;&#x961F;&#x5217;)&#xFF1A;&#x961F;&#x5217;&#x957F;&#x5EA6;&#x53D7;&#x9650;&#xFF0C;&#x5F53;&#x961F;&#x5217;&#x6EE1;&#x4E86;&#x5C31;&#x9700;&#x8981;&#x521B;&#x5EFA;&#x591A;&#x4F59;&#x7684;&#x7EBF;&#x7A0B;&#x6765;&#x6267;&#x884C;&#x4EFB;&#x52A1;</li>
</ul>
<h3 id="5&#x5E38;&#x89C1;&#x7EBF;&#x7A0B;&#x6C60;"><a name="5&#x5E38;&#x89C1;&#x7EBF;&#x7A0B;&#x6C60;" class="anchor-navigation-ex-anchor" href="#5&#x5E38;&#x89C1;&#x7EBF;&#x7A0B;&#x6C60;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="5&#x5E38;&#x89C1;&#x7EBF;&#x7A0B;&#x6C60;" class="plugin-anchor" href="#5&#x5E38;&#x89C1;&#x7EBF;&#x7A0B;&#x6C60;"><i class="fa fa-link" aria-hidden="true"></i></a>5.&#x5E38;&#x89C1;&#x7EBF;&#x7A0B;&#x6C60;</h3>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ThreadPoolDemo</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{

        ExecutorService executorService = Executors.newFixedThreadPool(<span class="hljs-number">4</span>);

        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
            <span class="hljs-keyword">int</span> index = i;
            executorService.submit(() -&gt; System.out.println(<span class="hljs-string">&quot;i:&quot;</span> + index +
                    <span class="hljs-string">&quot; executorService&quot;</span>));
        }
        executorService.shutdown();
    }
}
</code></pre>
<ul>
<li>&#x81EA;&#x52A8;&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x51E0;&#x79CD;&#x65B9;&#x5F0F;&#x90FD;&#x5C01;&#x88C5;&#x5728;Executors&#x5DE5;&#x5177;&#x7C7B;&#x4E2D;&#xFF1A;</li>
<li>newFixedThreadPool&#xFF1A;&#x4F7F;&#x7528;&#x7684;&#x6784;&#x9020;&#x65B9;&#x5F0F;&#x4E3A;new ThreadPoolExecutor(var0, var0, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue())&#xFF0C;&#x8BBE;&#x7F6E;&#x4E86;corePoolSize=maxPoolSize&#xFF0C;keepAliveTime=0(&#x6B64;&#x65F6;&#x8BE5;&#x53C2;&#x6570;&#x6CA1;&#x4F5C;&#x7528;)&#xFF0C;&#x65E0;&#x754C;&#x961F;&#x5217;&#xFF0C;&#x4EFB;&#x52A1;&#x53EF;&#x4EE5;&#x65E0;&#x9650;&#x653E;&#x5165;&#xFF0C;&#x5F53;&#x8BF7;&#x6C42;&#x8FC7;&#x591A;&#x65F6;(&#x4EFB;&#x52A1;&#x5904;&#x7406;&#x901F;&#x5EA6;&#x8DDF;&#x4E0D;&#x4E0A;&#x4EFB;&#x52A1;&#x63D0;&#x4EA4;&#x901F;&#x5EA6;&#x9020;&#x6210;&#x8BF7;&#x6C42;&#x5806;&#x79EF;)&#x53EF;&#x80FD;&#x5BFC;&#x81F4;&#x5360;&#x7528;&#x8FC7;&#x591A;&#x5185;&#x5B58;&#x6216;&#x76F4;&#x63A5;&#x5BFC;&#x81F4;OOM&#x5F02;&#x5E38;</li>
<li>newSingleThreadExector&#xFF1A;&#x4F7F;&#x7528;&#x7684;&#x6784;&#x9020;&#x65B9;&#x5F0F;&#x4E3A;new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), var0)&#xFF0C;&#x57FA;&#x672C;&#x540C;newFixedThreadPool&#xFF0C;&#x4F46;&#x662F;&#x5C06;&#x7EBF;&#x7A0B;&#x6570;&#x8BBE;&#x7F6E;&#x4E3A;&#x4E86;1&#xFF0C;&#x5355;&#x7EBF;&#x7A0B;&#xFF0C;&#x5F0A;&#x7AEF;&#x548C;newFixedThreadPool&#x4E00;&#x81F4;</li>
<li>newCachedThreadPool&#xFF1A;&#x4F7F;&#x7528;&#x7684;&#x6784;&#x9020;&#x65B9;&#x5F0F;&#x4E3A;new ThreadPoolExecutor(0, 2147483647, 60L, TimeUnit.SECONDS, new SynchronousQueue())&#xFF0C;corePoolSize=0&#xFF0C;maxPoolSize&#x4E3A;&#x5F88;&#x5927;&#x7684;&#x6570;&#xFF0C;&#x540C;&#x6B65;&#x79FB;&#x4EA4;&#x961F;&#x5217;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x4E0D;&#x7EF4;&#x62A4;&#x5E38;&#x9A7B;&#x7EBF;&#x7A0B;(&#x6838;&#x5FC3;&#x7EBF;&#x7A0B;)&#xFF0C;&#x6BCF;&#x6B21;&#x6765;&#x8BF7;&#x6C42;&#x76F4;&#x63A5;&#x521B;&#x5EFA;&#x65B0;&#x7EBF;&#x7A0B;&#x6765;&#x5904;&#x7406;&#x4EFB;&#x52A1;&#xFF0C;&#x4E5F;&#x4E0D;&#x4F7F;&#x7528;&#x961F;&#x5217;&#x7F13;&#x51B2;&#xFF0C;&#x4F1A;&#x81EA;&#x52A8;&#x56DE;&#x6536;&#x591A;&#x4F59;&#x7EBF;&#x7A0B;&#xFF0C;&#x7531;&#x4E8E;&#x5C06;maxPoolSize&#x8BBE;&#x7F6E;&#x6210;Integer.MAX_VALUE&#xFF0C;&#x5F53;&#x8BF7;&#x6C42;&#x5F88;&#x591A;&#x65F6;&#x5C31;&#x53EF;&#x80FD;&#x521B;&#x5EFA;&#x8FC7;&#x591A;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x5BFC;&#x81F4;&#x8D44;&#x6E90;&#x8017;&#x5C3D;OOM</li>
<li>newScheduledThreadPool&#xFF1A;&#x4F7F;&#x7528;&#x7684;&#x6784;&#x9020;&#x65B9;&#x5F0F;&#x4E3A;new ThreadPoolExecutor(var1, 2147483647, 0L, TimeUnit.NANOSECONDS, new ScheduledThreadPoolExecutor.DelayedWorkQueue())&#xFF0C;&#x652F;&#x6301;&#x5B9A;&#x65F6;&#x5468;&#x671F;&#x6027;&#x6267;&#x884C;&#xFF0C;&#x6CE8;&#x610F;&#x4E00;&#x4E0B;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x5EF6;&#x8FDF;&#x961F;&#x5217;&#xFF0C;&#x5F0A;&#x7AEF;&#x540C;newCachedThreadPool&#x4E00;&#x81F4;</li>
</ul>
<p>&#x6240;&#x4EE5;&#x6839;&#x636E;&#x4E0A;&#x9762;&#x5206;&#x6790;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;FixedThreadPool&#x548C;SigleThreadExecutor&#x4E2D;&#x4E4B;&#x6240;&#x4EE5;&#x7528;LinkedBlockingQueue&#x65E0;&#x754C;&#x961F;&#x5217;&#xFF0C;&#x662F;&#x56E0;&#x4E3A;&#x8BBE;&#x7F6E;&#x4E86;corePoolSize=maxPoolSize&#xFF0C;&#x7EBF;&#x7A0B;&#x6570;&#x65E0;&#x6CD5;&#x52A8;&#x6001;&#x6269;&#x5C55;&#xFF0C;&#x4E8E;&#x662F;&#x5C31;&#x8BBE;&#x7F6E;&#x4E86;&#x65E0;&#x754C;&#x963B;&#x585E;&#x961F;&#x5217;&#x6765;&#x5E94;&#x5BF9;&#x4E0D;&#x53EF;&#x77E5;&#x7684;&#x4EFB;&#x52A1;&#x91CF;&#xFF1B;&#x800C;CachedThreadPool&#x5219;&#x4F7F;&#x7528;&#x7684;&#x662F;SynchronousQueue&#x540C;&#x6B65;&#x79FB;&#x4EA4;&#x961F;&#x5217;&#xFF0C;&#x4E3A;&#x4EC0;&#x4E48;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x961F;&#x5217;&#x5462;&#xFF1F;&#x56E0;&#x4E3A;CachedThreadPool&#x8BBE;&#x7F6E;&#x4E86;corePoolSize=0&#xFF0C;maxPoolSize=Integer.MAX_VALUE&#xFF0C;&#x6765;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x5C31;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x6765;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#xFF0C;&#x7528;&#x4E0D;&#x5230;&#x961F;&#x5217;&#x6765;&#x5B58;&#x50A8;&#x4EFB;&#x52A1;&#xFF1B;SchduledThreadPool&#x7528;&#x7684;&#x662F;&#x5EF6;&#x8FDF;&#x961F;&#x5217;DelayedWorkQueue&#x3002;&#x5728;&#x5B9E;&#x9645;&#x9879;&#x76EE;&#x5F00;&#x53D1;&#x4E2D;&#x4E5F;&#x662F;&#x63A8;&#x8350;&#x4F7F;&#x7528;&#x624B;&#x52A8;&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;&#x6C60;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x800C;&#x4E0D;&#x7528;&#x9ED8;&#x8BA4;&#x65B9;&#x5F0F;&#xFF0C;&#x5173;&#x4E8E;&#x8FD9;&#x70B9;&#x5728;&#x300A;&#x963F;&#x91CC;&#x5DF4;&#x5DF4;&#x5F00;&#x53D1;&#x89C4;&#x8303;&#x300B;&#x4E2D;&#x662F;&#x8FD9;&#x6837;&#x63CF;&#x8FF0;&#x7684;&#xFF1A;</p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201211223422.png" alt="img"></p>
<p>&#x9075;&#x5FAA;&#x963F;&#x91CC;&#x5DF4;&#x5DF4;&#x7F16;&#x7801;&#x89C4;&#x8303;&#x7684;&#x63D0;&#x793A;&#xFF0C;&#x793A;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ThreadPoolDemo</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{

        ExecutorService executorService = <span class="hljs-keyword">new</span> ThreadPoolExecutor(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">0L</span>, 
                TimeUnit.MILLISECONDS, 
                <span class="hljs-keyword">new</span> LinkedBlockingQueue&lt;&gt;(<span class="hljs-number">10</span>), 
                Executors.defaultThreadFactory(), 
                <span class="hljs-keyword">new</span> ThreadPoolExecutor.AbortPolicy());

        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
            <span class="hljs-keyword">int</span> index = i;
            executorService.submit(() -&gt; System.out.println(<span class="hljs-string">&quot;i:&quot;</span> + index + 
                    <span class="hljs-string">&quot; executorService&quot;</span>));
        }
        executorService.shutdown();
    }
}
</code></pre>
<h3 id="6&#x62D2;&#x7EDD;&#x7B56;&#x7565;"><a name="6&#x62D2;&#x7EDD;&#x7B56;&#x7565;" class="anchor-navigation-ex-anchor" href="#6&#x62D2;&#x7EDD;&#x7B56;&#x7565;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="6&#x62D2;&#x7EDD;&#x7B56;&#x7565;" class="plugin-anchor" href="#6&#x62D2;&#x7EDD;&#x7B56;&#x7565;"><i class="fa fa-link" aria-hidden="true"></i></a>6.&#x62D2;&#x7EDD;&#x7B56;&#x7565;</h3>
<ul>
<li>AbortPolicy&#xFF1A;&#x4E2D;&#x65AD;&#x629B;&#x51FA;&#x5F02;&#x5E38;</li>
<li>DiscardPolicy&#xFF1A;&#x9ED8;&#x9ED8;&#x4E22;&#x5F03;&#x4EFB;&#x52A1;&#xFF0C;&#x4E0D;&#x8FDB;&#x884C;&#x4EFB;&#x4F55;&#x901A;&#x77E5;</li>
<li>DiscardOldestPolicy&#xFF1A;&#x4E22;&#x5F03;&#x6389;&#x5728;&#x961F;&#x5217;&#x4E2D;&#x5B58;&#x5728;&#x65F6;&#x95F4;&#x6700;&#x4E45;&#x7684;&#x4EFB;&#x52A1;</li>
<li>CallerRunsPolicy&#xFF1A;&#x8BA9;&#x63D0;&#x4EA4;&#x4EFB;&#x52A1;&#x7684;&#x7EBF;&#x7A0B;&#x53BB;&#x6267;&#x884C;&#x4EFB;&#x52A1;(&#x5BF9;&#x6BD4;&#x524D;&#x4E09;&#x79CD;&#x6BD4;&#x8F83;&#x53CB;&#x597D;&#x4E00;&#x4E22;&#x4E22;)</li>
</ul>
<h2 id="&#x4E8C;-execute&#x4E0E;submit"><a name="&#x4E8C;-execute&#x4E0E;submit" class="anchor-navigation-ex-anchor" href="#&#x4E8C;-execute&#x4E0E;submit"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E8C;-execute&#x4E0E;submit" class="plugin-anchor" href="#&#x4E8C;-execute&#x4E0E;submit"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E8C; execute&#x4E0E;submit</h2>
<h3 id="1&#x4F7F;&#x7528;submit&#x7684;&#x5751;"><a name="1&#x4F7F;&#x7528;submit&#x7684;&#x5751;" class="anchor-navigation-ex-anchor" href="#1&#x4F7F;&#x7528;submit&#x7684;&#x5751;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x4F7F;&#x7528;submit&#x7684;&#x5751;" class="plugin-anchor" href="#1&#x4F7F;&#x7528;submit&#x7684;&#x5751;"><i class="fa fa-link" aria-hidden="true"></i></a><strong>1.&#x4F7F;&#x7528;submit&#x7684;&#x5751;</strong></h3>
<p>&#x9996;&#x5148;&#x770B;&#x4E00;&#x4E0B;&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code>public class ThreadPoolDemo3 {

    public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(4);

        for (int i = 0; i &lt; 5; i++) {
            int index = i;
            executorService.submit(() -&gt; divTask(100, index));
        }
        executorService.shutdown();
    }
    private static void divTask(int a, int b) {
        double result = a / b;
        System.out.println(result);
    }
}
</code></pre><p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201211224904.png" alt="image-20201211224904022"></p>
<p>&#x3000;&#x3000;&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x4E3A;4&#x4E2A;&#xFF0C;&#x56E0;&#x8BE5;&#x662F;&#x6709;5&#x4E2A;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x5F53;<code>i=0</code>&#x7684;&#x65F6;&#x5019;&#xFF0C;<code>100/0</code>&#x662F;&#x4F1A;&#x62A5;&#x9519;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x65E5;&#x5FD7;&#x4FE1;&#x606F;&#x4E2D;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x4FE1;&#x606F;&#xFF0C;&#x662F;&#x4E3A;&#x4EC0;&#x4E48;&#x90A3;&#xFF1F;&#x5982;&#x679C;&#x4F7F;&#x7528;&#x4E86;<code>submit(Runnable task)</code> &#x5C31;&#x4F1A;&#x51FA;&#x73B0;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x4EFB;&#x4F55;&#x7684;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#x90FD;&#x51FA;&#x73B0;&#x4E0D;&#x4E86;&#xFF01;</p>
<p>&#x3000;&#x3000;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x4F7F;&#x7528;<code>submit(Runnable task)</code> &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x9519;&#x8BEF;&#x7684;&#x5806;&#x6808;&#x4FE1;&#x606F;&#x8DD1;&#x51FA;&#x6765;&#x7684;&#x65F6;&#x5019;&#x4F1A;&#x88AB;&#x5185;&#x90E8;&#x6355;&#x83B7;&#x5230;&#xFF0C;&#x6240;&#x4EE5;&#x6253;&#x5370;&#x4E0D;&#x51FA;&#x6765;&#x5177;&#x4F53;&#x7684;&#x4FE1;&#x606F;&#x8BA9;&#x6211;&#x4EEC;&#x67E5;&#x770B;&#xFF0C;&#x89E3;&#x51B3;&#x7684;&#x65B9;&#x6CD5;&#x6709;&#x5982;&#x4E0B;&#x4E24;&#x79CD;&#xFF1A;</p>
<ul>
<li><strong>1&#x3001;&#x4F7F;&#x7528;execute&#xFF08;&#xFF09;&#x4EE3;&#x66FF;submit&#xFF08;&#xFF09;&#xFF1B;</strong></li>
</ul>
<pre><code>public class ThreadPoolDemo3 {

    public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(4);

        for (int i = 0; i &lt; 5; i++) {
            int index = i;
            executorService.execute(() -&gt; divTask(100, index));
        }
        executorService.shutdown();
    }

    private static void divTask(int a, int b) {
        double result = a / b;
        System.out.println(result);
    }
}
</code></pre><p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201211224940.png" alt="image-20201211224940100"></p>
<ul>
<li><strong>2&#x3001;&#x4F7F;&#x7528;Future</strong></li>
</ul>
<pre><code>public class ThreadPoolDemo3 {

    public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(4);

        for (int i = 0; i &lt; 5; i++) {
            int index = i;
            Future future = executorService.submit(() -&gt; divTask(200, index));
            try {
                future.get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }
        executorService.shutdown();
    }

    private static void divTask(int a, int b) {
        double result = a / b;
        System.out.println(result);
    }
}
</code></pre><p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201211225014.png" alt="image-20201211225014471"></p>
<h3 id="3-execute&#x548C;submit&#x7684;&#x533A;&#x522B;"><a name="3-execute&#x548C;submit&#x7684;&#x533A;&#x522B;" class="anchor-navigation-ex-anchor" href="#3-execute&#x548C;submit&#x7684;&#x533A;&#x522B;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="3-execute&#x548C;submit&#x7684;&#x533A;&#x522B;" class="plugin-anchor" href="#3-execute&#x548C;submit&#x7684;&#x533A;&#x522B;"><i class="fa fa-link" aria-hidden="true"></i></a>3. execute&#x548C;submit&#x7684;&#x533A;&#x522B;</h3>
<p>&#xFF08;1&#xFF09;<code>execute()</code>&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x63D0;&#x4EA4;&#x4E0D;&#x9700;&#x8981;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x6240;&#x4EE5;&#x65E0;&#x6CD5;&#x5224;&#x65AD;&#x4EFB;&#x52A1;&#x662F;&#x5426;&#x88AB;&#x7EBF;&#x7A0B;&#x6C60;&#x6267;&#x884C;&#x6210;&#x529F;&#x3002;&#x901A;&#x8FC7;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#x53EF;&#x77E5;<code>execute()</code>&#x65B9;&#x6CD5;&#x8F93;&#x5165;&#x7684;&#x4EFB;&#x52A1;&#x662F;&#x4E00;&#x4E2A;Runnable&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x3002;</p>
<p>&#xFF08;2&#xFF09;<code>submit()</code>&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x63D0;&#x4EA4;&#x9700;&#x8981;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x4EFB;&#x52A1;&#x3002;&#x7EBF;&#x7A0B;&#x6C60;&#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;future&#x7C7B;&#x578B;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x901A;&#x8FC7;&#x8FD9;&#x4E2A;future&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x5224;&#x65AD;&#x4EFB;&#x52A1;&#x662F;&#x5426;&#x6267;&#x884C;&#x6210;&#x529F;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;future&#x7684;<code>get()</code>&#x65B9;&#x6CD5;&#x6765;&#x83B7;&#x53D6;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;<code>get()</code>&#x65B9;&#x6CD5;&#x4F1A;&#x963B;&#x585E;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x76F4;&#x5230;&#x4EFB;&#x52A1;&#x5B8C;&#x6210;&#xFF0C;&#x800C;&#x4F7F;&#x7528;<code>get&#xFF08;long timeout&#xFF0C;TimeUnit unit&#xFF09;</code>&#x65B9;&#x6CD5;&#x5219;&#x4F1A;&#x963B;&#x585E;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#x540E;&#x7ACB;&#x5373;&#x8FD4;&#x56DE;&#xFF0C;&#x8FD9;&#x65F6;&#x5019;&#x6709;&#x53EF;&#x80FD;&#x4EFB;&#x52A1;&#x6CA1;&#x6709;&#x6267;&#x884C;&#x5B8C;&#x3002;</p>
<h2 id="&#x4E09;-&#x7EBF;&#x7A0B;&#x6C60;&#x7EBF;&#x7A0B;&#x590D;&#x7528;&#x539F;&#x7406;"><a name="&#x4E09;-&#x7EBF;&#x7A0B;&#x6C60;&#x7EBF;&#x7A0B;&#x590D;&#x7528;&#x539F;&#x7406;" class="anchor-navigation-ex-anchor" href="#&#x4E09;-&#x7EBF;&#x7A0B;&#x6C60;&#x7EBF;&#x7A0B;&#x590D;&#x7528;&#x539F;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E09;-&#x7EBF;&#x7A0B;&#x6C60;&#x7EBF;&#x7A0B;&#x590D;&#x7528;&#x539F;&#x7406;" class="plugin-anchor" href="#&#x4E09;-&#x7EBF;&#x7A0B;&#x6C60;&#x7EBF;&#x7A0B;&#x590D;&#x7528;&#x539F;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E09; &#x7EBF;&#x7A0B;&#x6C60;&#x7EBF;&#x7A0B;&#x590D;&#x7528;&#x539F;&#x7406;</h2>
<h3 id="1&#x6838;&#x5FC3;&#x539F;&#x7406;"><a name="1&#x6838;&#x5FC3;&#x539F;&#x7406;" class="anchor-navigation-ex-anchor" href="#1&#x6838;&#x5FC3;&#x539F;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x6838;&#x5FC3;&#x539F;&#x7406;" class="plugin-anchor" href="#1&#x6838;&#x5FC3;&#x539F;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a>1.&#x6838;&#x5FC3;&#x539F;&#x7406;</h3>
<p>&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E00;&#x822C;&#x5728;&#x6267;&#x884C;&#x5B8C;&#x4EFB;&#x52A1;&#x540E;&#x5C31;&#x7ED3;&#x675F;&#x4E86;&#xFF0C;&#x600E;&#x4E48;&#x518D;&#x8BA9;&#x4ED6;&#x6267;&#x884C;&#x4E0B;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x5462;&#xFF1F;&#x3000;</p>
<p>&#x3000;&#x3000;&#x7EBF;&#x7A0B;&#x91CD;&#x7528;&#x7684;&#x6838;&#x5FC3;&#x662F;&#xFF0C;&#x6211;&#x4EEC;&#x77E5;&#x9053;&#xFF0C;Thread.start()&#x53EA;&#x80FD;&#x8C03;&#x7528;&#x4E00;&#x6B21;&#xFF0C;&#x4E00;&#x65E6;&#x8FD9;&#x4E2A;&#x8C03;&#x7528;&#x7ED3;&#x675F;&#xFF0C;&#x5219;&#x8BE5;&#x7EBF;&#x7A0B;&#x5C31;&#x5230;&#x4E86;stop&#x72B6;&#x6001;&#xFF0C;&#x4E0D;&#x80FD;&#x518D;&#x6B21;&#x8C03;&#x7528;start&#x3002;
&#x5219;&#x8981;&#x8FBE;&#x5230;&#x590D;&#x7528;&#x7684;&#x76EE;&#x7684;&#xFF0C;&#x5219;&#x5FC5;&#x987B;&#x4ECE;Runnable&#x63A5;&#x53E3;&#x7684;run()&#x65B9;&#x6CD5;&#x4E0A;&#x5165;&#x624B;&#xFF0C;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x8BBE;&#x8BA1;&#x8FD9;&#x4E2A;Runnable.run()&#x65B9;&#x6CD5;&#xFF08;&#x5C31;&#x53EB;&#x5916;&#x9762;&#x7684;run()&#x65B9;&#x6CD5;&#xFF09;&#xFF1A;
&#x5B83;&#x672C;&#x8D28;&#x4E0A;&#x662F;&#x4E2A;&#x65E0;&#x9650;&#x5FAA;&#x73AF;&#xFF0C;&#x8DD1;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x4E0D;&#x65AD;&#x68C0;&#x67E5;&#x6211;&#x4EEC;&#x662F;&#x5426;&#x6709;&#x65B0;&#x52A0;&#x5165;&#x7684;&#x5B50;Runnable&#x5BF9;&#x8C61;&#xFF08;&#x5C31;&#x53EB;&#x5185;&#x90E8;&#x7684;runnable:run()&#x5427;&#xFF0C;&#x5B83;&#x5C31;&#x662F;&#x7528;&#x6765;&#x5B9E;&#x73B0;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x7684;&#x4EFB;&#x52A1;&#xFF09;&#xFF0C;&#x6709;&#x5C31;&#x8C03;&#x4E00;&#x4E0B;&#x6211;&#x4EEC;&#x7684;run()&#xFF0C;&#x5176;&#x5B9E;&#x5C31;&#x4E00;&#x4E2A;&#x5927;run()&#x628A;&#x5176;&#x5B83;&#x5C0F;run()#1,run()#2,...&#x7ED9;&#x4E32;&#x8054;&#x8D77;&#x6765;&#x4E86;&#xFF0C;&#x57FA;&#x672C;&#x539F;&#x7406;&#x5C31;&#x8FD9;&#x4E48;&#x7B80;&#x5355;
&#x4E0D;&#x505C;&#x5730;&#x5904;&#x7406;&#x6211;&#x4EEC;&#x63D0;&#x4EA4;&#x7684;Runnable&#x4EFB;&#x52A1;&#x3002;</p>
<pre><code>public void run() {
    while(true) {
        if(tasks available) {
           Runnable task = taskqueue.dequeue();
           task.run();
        } else {
           // wait or whatever
        }
    }
}
</code></pre><p>&#x3000;&#x4E0B;&#x9762;&#x4E3E;&#x4E2A;&#x4EE3;&#x7801;&#x5B9E;&#x4F8B;&#x6765;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x7EBF;&#x7A0B;&#x6C60;&#x590D;&#x7528;&#x7EBF;&#x7A0B;</p>
<p>&#x3000;&#x3000;&#x751F;&#x4EA7;&#x4E86;&#x4E24;&#x4E2A; &#x7EBF;&#x7A0B;&#x4F5C;&#x4E3A;&#x5DE5;&#x4EBA;</p>
<p>&#x3000;&#x3000;&#x751F;&#x4EA7;&#x4E86;10&#x4E2A;&#x540C;&#x6837;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x8BA9;&#x4ED6;&#x4EEC;&#x6267;&#x884C;</p>
<p>&#x3000;&#x3000;&#x5229;&#x7528;&#x590D;&#x7528;&#x8BA9; 2&#x4E2A;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;10&#x4E2A;&#x4EFB;&#x52A1;</p>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> java.util.ArrayList;
<span class="hljs-keyword">import</span> java.util.LinkedList;
<span class="hljs-keyword">import</span> java.util.concurrent.TimeUnit;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Mythreadpool</span> </span>{
    LinkedList&lt;Task&gt; taskList = <span class="hljs-keyword">new</span> LinkedList&lt;Task&gt;();

    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Task</span> </span>{ <span class="hljs-comment">//&#x4EFB;&#x52A1;&#x7C7B;</span>
        <span class="hljs-keyword">int</span> id;
        Task(<span class="hljs-keyword">int</span> id){
            <span class="hljs-keyword">this</span>.id=id;
            System.out.println(<span class="hljs-string">&quot;&#x7B2C;&quot;</span>+id+<span class="hljs-string">&quot;&#x4E2A;&#x4EFB;&#x52A1;&#x4EA7;&#x751F;&quot;</span>);
        }
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{<span class="hljs-comment">//&#x5177;&#x4F53;&#x7684;&#x5DE5;&#x4F5C;</span>
            System.out.println(<span class="hljs-string">&quot;&#x7B2C;&quot;</span>+id+<span class="hljs-string">&quot;&#x4E2A;&#x4EFB;&#x52A1;&#x6B63;&#x5728;&#x6267;&#x884C;&quot;</span>);
            <span class="hljs-keyword">try</span> {
                TimeUnit.SECONDS.sleep(<span class="hljs-number">1</span>);
            } <span class="hljs-keyword">catch</span> (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(<span class="hljs-string">&quot;&#x7B2C;&quot;</span>+id+<span class="hljs-string">&quot;&#x4E2A;&#x4EFB;&#x52A1;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&quot;</span>);
        }
    }

    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Worker</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Thread</span> </span>{ <span class="hljs-comment">//&#x5DE5;&#x4EBA;&#x5B9E;&#x4F53;</span>
        String name;
        Worker(String name) {
            <span class="hljs-keyword">this</span>.name = name;
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">while</span>(<span class="hljs-keyword">true</span>) {
                <span class="hljs-keyword">if</span>(taskList.size() == <span class="hljs-number">0</span>) {
                    <span class="hljs-keyword">try</span> {
                        <span class="hljs-keyword">synchronized</span> (taskList) {
                            System.out.println(<span class="hljs-string">&quot;Worker &quot;</span> + name+<span class="hljs-string">&quot; &#x6CA1;&#x6709;&#x4EFB;&#x52A1;&quot;</span>);
                            taskList.wait(); <span class="hljs-comment">//&#x6CA1;&#x5F97;&#x5230;&#x4EFB;&#x52A1;&#xFF0C;&#x8FDB;&#x5165;tasklist&#x7684;&#x7B49;&#x5F85;&#x961F;&#x5217;</span>
                        }
                    } <span class="hljs-keyword">catch</span> (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                <span class="hljs-keyword">synchronized</span> (taskList) {
                    System.out.println(<span class="hljs-string">&quot;Worker &quot;</span> + name+<span class="hljs-string">&quot; &#x5F97;&#x5230;&#x4EFB;&#x52A1;&quot;</span>);
                    taskList.removeFirst().run();
                }
            }
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">pool</span><span class="hljs-params">()</span> </span>{  <span class="hljs-comment">//&#x5DE5;&#x4EBA;&#x3002;&#x53EA;&#x751F;&#x4EA7;&#x4E86;&#x4E24;&#x4E2A;&#x5DE5;&#x4EBA;</span>
         ArrayList&lt;Worker&gt; wokerlist=<span class="hljs-keyword">new</span> ArrayList&lt;Worker&gt;();
         <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i&lt;<span class="hljs-number">2</span>;i++) {
             Worker k = <span class="hljs-keyword">new</span> Worker(<span class="hljs-string">&quot;&#x7B2C;&quot;</span>+(i+<span class="hljs-number">1</span>)+<span class="hljs-string">&quot;&#x4E2A;&#x5DE5;&#x4EBA;&quot;</span>);
             k.start();
             wokerlist.add(k);<span class="hljs-comment">//</span>
         }
    }

    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Factory</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Thread</span></span>{ <span class="hljs-comment">//&#x751F;&#x4EA7;&#x4EFB;&#x52A1;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x603B;&#x5171;&#x4F1A;&#x751F;&#x4EA7;10&#x4E2A;&#x4EFB;&#x52A1;</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i&lt;<span class="hljs-number">10</span>;i++) {
                <span class="hljs-keyword">synchronized</span>(taskList) {
                    taskList.addLast(<span class="hljs-keyword">new</span> Task(i+<span class="hljs-number">1</span>));
                    taskList.notify();
                }
                <span class="hljs-keyword">try</span> {
                    TimeUnit.SECONDS.sleep(<span class="hljs-number">1</span>);
                } <span class="hljs-keyword">catch</span> (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        Mythreadpool mythreadpool = <span class="hljs-keyword">new</span> Mythreadpool();
        mythreadpool.pool(); <span class="hljs-comment">//&#x521D;&#x59CB;&#x5316;&#x5DE5;&#x4EBA;</span>
        Mythreadpool.Factory m= mythreadpool.new Factory();
        m.start();
    }
}
</code></pre>
<h3 id="2jdk&#x7EBF;&#x7A0B;&#x6C60;&#x590D;&#x7528;"><a name="2jdk&#x7EBF;&#x7A0B;&#x6C60;&#x590D;&#x7528;" class="anchor-navigation-ex-anchor" href="#2jdk&#x7EBF;&#x7A0B;&#x6C60;&#x590D;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2jdk&#x7EBF;&#x7A0B;&#x6C60;&#x590D;&#x7528;" class="plugin-anchor" href="#2jdk&#x7EBF;&#x7A0B;&#x6C60;&#x590D;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>2.jdk&#x7EBF;&#x7A0B;&#x6C60;&#x590D;&#x7528;</h3>
<p><strong>ThreadPoolExecutor.execute()</strong></p>
<pre><code>&#x5206;&#x6790;&#xFF0C;getTask&#x662F;&#x4ECE;&#x7EBF;&#x7A0B;&#x6C60;&#x4E2D;&#xFF0C;&#x83B7;&#x53D6;&#x7684;&#x4EFB;&#x52A1;&#x3002;&#x5373;&#x6240;&#x6709;&#x7684;&#x4EFB;&#x52A1;&#x90FD;&#x653E;&#x5728;ThreadPoolExecutor&#x4E2D;&#xFF0C;&#x7EBF;&#x7A0B;&#x6C60;&#x542F;&#x52A8;&#x591A;&#x4E2A;Worker&#x53BB;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#xFF0C;&#x6BCF;&#x4E2A;worker&#x4E0D;&#x505C;&#x7684;&#x4ECE;ThreadPoolExector&#x7684;workQueue&#x4E2D;&#x53D6;&#x51FA;&#x4EFB;&#x52A1;&#xFF0C;&#x6267;&#x884C;task.run()&#x65B9;&#x6CD5;&#xFF0C;&#x76F4;&#x81F3;&#x6240;&#x6709;&#x7684;&#x4EFB;&#x52A1;&#x6267;&#x884C;&#x5B8C;&#x6BD5;
</code></pre>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../blockingQueue/1.阻塞队列.html" class="navigation navigation-prev " aria-label="Previous page: 10.阻塞队列.md">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../10面试题一.html" class="navigation navigation-next " aria-label="Next page: 12.面试题一">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"11.线程池与底层原理.md","level":"6.12","depth":1,"next":{"title":"12.面试题一","level":"6.13","depth":1,"path":"多线程/10面试题一.md","ref":"多线程/10面试题一.md","articles":[]},"previous":{"title":"10.阻塞队列.md","level":"6.11","depth":1,"path":"多线程/blockingQueue/1.阻塞队列.md","ref":"多线程/blockingQueue/1.阻塞队列.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","splitter","expandable-chapters-small","anchors","sharing-plus","anchor-navigation-ex","favicon","expandable-chapters","toggle-chapters","back-to-top-button","code","-lunr","-search","search-pro","pageview-count","popup","livereload"],"styles":{"website":"./styles/website.css"},"pluginsConfig":{"livereload":{},"splitter":{},"search-pro":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"popup":{},"code":{"copyButtons":true},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":false},"favicon":{},"back-to-top-button":{},"pageview-count":{},"expandable-chapters-small":{},"sharing":{"qq":false,"all":["google","facebook","weibo","twitter","qq","qzone","linkedin","pocket"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{},"toggle-chapters":{},"expandable-chapters":{}},"theme":"default","author":"hzh","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"计算机 ,java , 大数据 基础知识体系","language":"zh-hans","gitbook":"3.2.3"},"file":{"path":"多线程/pond/1.线程池与底层原理.md","mtime":"2020-12-20T15:31:59.182Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-12-30T11:12:56.611Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

